/**
 *  请求封装
 */
import baseUrl from './config.js';


export default function({
	url,
	method,
	data
}) {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '加载中'
		})
		uni.request({
			url: baseUrl + url,
			method,
			data,
			header: {
				token: uni.getStorageSync("token")
			},
			timeout: 5000,
			success(res) {
				if (res.data.code == 20002) {
					// 移除本地保存的token
					uni.removeStorageSync('token');
					uni.showModal({
						title: '提示',
						content: res.data.message,
						success({
							confirm
						}) {
							console.log('confirm', confirm);
							if (confirm) {
								uni.navigateTo({
									url: '/pages/login/login'
								})
							} else {
								uni.showToast({
									title: '取消了!'
								})
							}
						}
					})
				} else {
					resolve(res.data);
				}
				uni.hideLoading();
			},
			fail(err) {
				reject(err);
				uni.hideLoading();
			}
		})
	});
}
